package commons.base.elasticsearch;

import java.net.InetSocketAddress;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

public class ESClient {

	private Client client;
	
	public ESClient(String clusterName,String host, int port){
		Settings settings = Settings.builder()
				// 指定集群名称
				.put("cluster.name", clusterName)
				// 探测集群中机器状态
				.put("client.transport.sniff", true).build();

		client = TransportClient.builder().settings(settings).build()
				.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(host, port)));
	}
	
	public IndexResponse add(String index, String type, XContentBuilder sourceBuilder) {
		return client.prepareIndex(index, type).setSource(sourceBuilder).execute().actionGet();
	}
	
	public IndexResponse update(String index, String type, String id, XContentBuilder sourceBuilder) {
		return client.prepareIndex(index, type).setId(id).setSource(sourceBuilder).execute().actionGet();
	}
	
	public BulkOp newBulk(int maxSize) {
		return new BulkOp(client, maxSize);
	}
	
	public DeleteResponse delete(String index, String type, String id) {
		return client.prepareDelete(index, type, id).execute().actionGet();
	}
	
	public GetResponse get(String index, String type, String id) {
		return client.prepareGet(index, type, id).execute().actionGet();
	}
	
	/**
	 * 单字段全文搜索
	 * @param index
	 * @param type
	 * @param name
	 * @param text
	 * @return
	 */
	public SearchResponse search(String index, String type, int size, String name, String text) {
		QueryBuilder qb = QueryBuilders.matchQuery(name, text);
		
		return client.prepareSearch(index).setScroll(new TimeValue(60000)).setQuery(qb)
//				.addHighlightedField(name)
				.setSize(size).execute().actionGet();
	}
	
	/**
	 * 多字段全文搜索
	 * @param index
	 * @param type
	 * @param text
	 * @param fieldNames
	 * @return
	 */
	public SearchResponse multiSearch(String index, String type, int size, String text, String... fieldNames) {
		QueryBuilder qb = QueryBuilders.multiMatchQuery(text, fieldNames);
		
		return client.prepareSearch(index).setScroll(new TimeValue(60000)).setQuery(qb)
				.setSize(size).execute().actionGet();
	}
	
	/**
	 * 可用于组合查询
	 * 如：QueryBuilders.boolQuery()
	 *     .must(QueryBuilders.termQuery("userId", "1"))
	 *     .must(QueryBuilders.rangeQuery("time").gt("2016-06-01 00:00:00").lt("2016-06-01 23:59:59"));
	 * @param index
	 * @param type
	 * @param queryBuilder
	 * @param size
	 * @return
	 */
	public SearchResponse search(String index, String type, QueryBuilder queryBuilder, int size) {
		return client.prepareSearch(index).setScroll(new TimeValue(60000)).setQuery(queryBuilder)
				.setSize(size).execute().actionGet();
	}
	
	public Client getClient() {
		return client;
	}

	public void close(){
		if(client != null){
			client.close();
		}
	}
}
